#!/bin/bash
clientId="1617584765828"

txtblaground="\033[0m" # White 白字黑底
txtblagroundground="\033[47;30m" # 白底黑字
txtblugroundground="\033[46;30m" # 蓝底黑字

##yumInstall
function killSelf(){
  ps -ef |grep `echo $0`|grep -v grep|awk -F ' ' '{print $2}'|xargs kill -9
}
##killSelf
function pass(){
  echo '' >> /dev/null
}
function checkFileExist() {
    if [ -f ${1} ]; then
      echo true
    else
      echo false
    fi
}
function jq() {
    if [ $(checkFileExist /usr/bin/jq) == true  ]; then
    #echo 123
    pass
    else
      wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
      chmod +x ./jq
      mv jq /usr/bin
    fi
}
jq #解决jq不存在的问题:读取json用

function askTaskAndExec() {
  echo 主任务id: ${mainTaskId}

          curl --request POST -sL \
           --url 'http://devops-test.36ve.com/javacmdb/clientServlet?action=getStatusMakeBasicBehavior' -d "clientId=${clientId}" -d "mainTaskId=${mainTaskId}" --output a.log
      cat a.log|grep javacmdb >> /dev/null
      if [ $? == 0 ]; then
        return
      else
        pass
      fi #这是屏蔽服务器返回故障的操作
      echo 实际行为对象为: $(cat a.log)

      if [ $(grep "上一个模块跟下一个模块不在同一个server上" a.log)x == ""x ]; then
        pass
      else
        echo "下一个模块不在本实例上,可以退出了";return
      fi

      if [ $(grep "你所请求的主任务已经做完了,而且没有后续任务可做了,休息一下吧" a.log)x == ""x  ]; then
        pass
      else
        echo "你所请求的主任务已经做完了,而且没有后续任务可做了,休息一下吧";return
      fi

      if [ -s a.log ]; then
        pass
      else
        echo responseEmpty;return ;
      fi

      command=$(/usr/bin/jq -r '.command' a.log)
      #command='ls a.log'
      #cat a.log

      echo -e "${txtblugroundground}"命令为: "${command}""${txtblaground}"

      #echo -e "\033[41;37m $module的日志已经放入$module_log_bak_dir########## \033[0m"
      mainTaskId=$(/usr/bin/jq -r '.mainTaskId' a.log)

      #$(${command}>commandResult.log)
cat >  x.sh << EOF
 ${command} >commandResult.log
EOF

      chmod +x x.sh
      ./x.sh
      commandStatus=$?
      rm -f x.sh
      echo 状态为: "${commandStatus}"
      echo 命令结果为: $(cat commandResult.log) ++++++++++++++++++++++++++++

      unicodeCommandResult=$(cat commandResult.log  | xxd -plain | sed 's/\(..\)/%\1/g') #把中文,空格等转换成unicode编码,服务器会自动解码的
      id=$(/usr/bin/jq -r '.id' a.log)
      curl -sL -d "action=reportBasicBehavior" -d "clientId=${clientId}" -d "id=${id}"  -d "status=${commandStatus}"  -d "result=${unicodeCommandResult}" \
      'http://devops-test.36ve.com/javacmdb/clientServlet' >reportBasicBehavior.log

      cat reportBasicBehavior.log|grep javacmdb >> /dev/null
      if [ $? == 0 ]; then
        echo 回信有异常
        cat reportBasicBehavior.log
        return
      else
        pass
      fi #这是屏蔽服务器返回故障的操作

      result502=$(grep 502 reportBasicBehavior.log)
      if [ "${result502}"x != ""x ]; then
          echo serverError:502;return
      fi

      echo  reportBasicBehavior的内容是:$(cat reportBasicBehavior.log)
      if [ $(cat reportBasicBehavior.log)x == yesx ]; then
        echo 要在进入递归了
        askTaskAndExec
      elif [ $(cat reportBasicBehavior.log)x == nox ]; then
          echo 没有需要执行的任务
          return
      elif [ $(cat reportBasicBehavior.log)x == errorx ]; then
          echo 上一条执行状态ok,但是result结果判定异常:error
          return
      fi
}
while [ 1 -le 2 ]; do
    #curl --request GET -sL \
    #     --url 'http://devops-test.36ve.com/javacmdb/taskServlet?action=existTask'\
    #     --output '/lhwl/bin/tmp/ggctest.log'
    #echo 1

    existTaskResult=$(curl --request POST -sL -d "clientId=${clientId}"\
     --url 'http://devops-test.36ve.com/javacmdb/clientServlet?action=existBasicBehavior' )

      result502=$(echo ${existTaskResult}|grep 502)
      if [ "${result502}"x != ""x ]; then
          echo serverError:502;sleep 2;continue
      fi
    echo "${existTaskResult}"
    mainTaskId=""
    #exit
    if [ "${existTaskResult}"x == "yesx" ]; then
      #如果它显示有任务就去请求任务
      #echo "有"
      askTaskAndExec
      echo quitFrom"askTaskAndExec"
    elif [ "${existTaskResult}"x == "nox" ]; then
        echo "没有需要执行的任务"
        #echo 退出后主任务id: ${mainTaskId}
    fi

    sleep 3
done







